﻿using System;

using HsTec.Mobile.Green.BLL;
using HsTec.Mobile.Green.Model;

public partial class ReportTemplate_TemplateTree_Right : System.Web.UI.Page
{
    private string sid = String.Empty;  //字段节点ID
    private ReportTemplateBLL bll = new ReportTemplateBLL();
    private string name = String.Empty;
    private string parent = String.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        btnhtmlsave.Attributes.Add("onclick", "addfield()");
        try
        {
            sid = this.Request.QueryString["id"].ToString().Trim();
        }
        catch
        {

        }
        if (!IsPostBack)
        {
            if (!String.IsNullOrEmpty(sid))
            {
                //判断选中的文件
                if (sid != "0")
                {
                    //选中的数据表或者字段
                    ReportTemplate rt = bll.GetModel(int.Parse(sid));
                    txtFieldName.Value = rt.FieldName;
                    sltFieldType.Value = rt.FieldType.ToString();
                    txtAlias.Value = rt.FieldAlias;
                    if (rt.FieldType == 4)
                        slttr.Style.Add("display", "");
                    else
                        slttr.Style.Add("display", "none");
                    sltConent.DataSource = bll.getContent(rt.SelectContent);
                    sltConent.DataBind();
                    txtFieldExcel.Value = rt.EecelCell;
                    txtNumber.Value = rt.FieldNo.ToString();
                    hidParent.Value = rt.FieldParent.ToString();
                    taRemark.Value = rt.Remark;
                    //if (hidParent.Value == "0")
                    if (rt.FieldType == 0)
                        btnTemplate.Visible = true;
                    else
                        btnTemplate.Visible = false;
                    //查询状态
                    if (String.IsNullOrEmpty(hidOperation.Value))
                    {
                        labEdit.InnerText = "查看字段";
                        txtFieldName.Disabled = true;
                        sltFieldType.Disabled = true;
                        sltConent.Disabled = true;
                        txtFieldExcel.Disabled = true;
                        txtNumber.Disabled = true;
                        taRemark.Disabled = true;
                        txtAlias.Disabled = true;
                    }
                    if (rt.FieldParent == 0)
                        sltField.Visible = false;
                    else
                        sltField.Visible = true;
                }
                else
                {
                    //选中的根目录 只能增加表
                    labEdit.InnerText = "查看表";
                    labName.InnerText = "数据表名：";
                    txtFieldName.Disabled = true;
                    sltFieldType.Disabled = true;
                    sltConent.Disabled = true;
                    txtFieldExcel.Disabled = true;
                    txtNumber.Disabled = true;
                    taRemark.Disabled = true;
                    btnEdit.Visible = false;
                    btnDel.Visible = false;
                    sltField.Visible = false;
                    txtAlias.Disabled = true;
                }
            }
            else
            {
                labEdit.Visible = false;
                labName.InnerText = "数据表名：";
                txtFieldName.Disabled = true;
                sltFieldType.Disabled = true;
                sltConent.Disabled = true;
                txtFieldExcel.Disabled = true;
                txtNumber.Disabled = true;
                taRemark.Disabled = true;
                btnEdit.Visible = true;
                btnDel.Visible = true;
                sltField.Visible = true;
                txtAlias.Disabled = true;
                sltField.Visible = false;
                btnAdd.Visible = false;
                btnDel.Visible = false;
                btnEdit.Visible = false;
            }
            btnhtmlsave.Visible = false;
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        ReportTemplate rt = new ReportTemplate();
        if (hidOperation.Value == "edit")
            rt = bll.GetModel(int.Parse(sid));
        rt.FieldName = txtFieldName.Value;
        rt.FieldType = int.Parse(sltFieldType.Value);
        rt.SelectContent = hidContent.Value;
        rt.EecelCell = txtFieldExcel.Value;
        rt.Remark = taRemark.Value;
        rt.FieldNo = txtNumber.Value == String.Empty ? 0 : int.Parse(txtNumber.Value);
        rt.FieldAlias = txtAlias.Value;
        int op = 0;
        if (hidOperation.Value == "add")
        {
            //设置字段模板
            ReportTemplate parent = bll.GetModel(int.Parse(sid));
            if (parent == null)
                rt.FieldTemplate = 0;
            else
            {
                if (parent.FieldParent == 0)
                    rt.FieldTemplate = int.Parse(sid);
                else
                    rt.FieldTemplate = parent.FieldTemplate;
            }
            rt.FieldParent = int.Parse(sid);
            op = bll.Add(rt);
        }
        else
        {
            op = bll.Update(rt);
        }
        if (op == 1)
        {

            if (hidOperation.Value == "add")
            {
                hidOperation.Value = String.Empty;
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存成功!');var hid = parent.leftFrame.document.getElementById('hidSelectNode'); if(hid!=null) hid.value = '" + sid + "'; var btn = parent.leftFrame.document.getElementById('reflashNode');if(btn!=null)btn.click();self.location='TemplateTree_Right.aspx?id=" + sid + "';</script>");
            }
            else
            {
                hidOperation.Value = String.Empty;
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存成功!');var hid = parent.leftFrame.document.getElementById('hidSelectNode'); if(hid!=null) hid.value = '" + hidParent.Value + "'; var btn = parent.leftFrame.document.getElementById('reflashNode');if(btn!=null)btn.click();self.location='TemplateTree_Right.aspx?id=" + sid + "';</script>");
            }
            return;
        }
        else if (op == 2)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('该字段已经存在!请修改字段名后重新保存!');</script>");
            return;
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存失败!');</script>");
            return;
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        hidOperation.Value = "add";
        labEdit.InnerText = "增加字段";
        txtFieldName.Value = String.Empty;
        sltFieldType.Value = String.Empty;
        sltConent.Items.Clear();
        txtAlias.Value = String.Empty;
        txtFieldExcel.Value = String.Empty;
        txtNumber.Value = String.Empty;
        taRemark.Value = String.Empty;
        hidbtn();
        sltField.Visible = true;
        if (sid == "0")
            sltField.Visible = false;
    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        hidOperation.Value = "edit";
        labEdit.InnerText = "修改字段";
        hidbtn();
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        hidOperation.Value = String.Empty;
        Response.Redirect("TemplateTree_Right.aspx?id=" + sid);
    }
    private void hidbtn()
    {
        btnhtmlsave.Visible = true;
        btnCancel.Visible = true;
        btnEdit.Visible = false;
        btnDel.Visible = false;
        btnAdd.Visible = false;
        btnTemplate.Visible = false;
        txtFieldName.Disabled = false;
        sltFieldType.Disabled = false;
        sltConent.Disabled = false;
        txtFieldExcel.Disabled = false;
        txtNumber.Disabled = false;
        taRemark.Disabled = false;
        txtAlias.Disabled = false;
    }
    /// <summary>
    /// 生成模板按钮事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnTemplate_Click(object sender, EventArgs e)
    {
        bll.GenerationTemplates(sid);
        // 生成对应HTML格式
        //bll.GenerationForm(int.Parse(sid));
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        int iback = bll.Delete(int.Parse(sid));
        if (iback == 1)
        {
            ReportTemplate model = bll.GetModel(int.Parse(sid));
            hidOperation.Value = String.Empty;
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存成功!');parent.leftFrame.document.location='ProjectManage_Left.aspx.aspx?id=" + model.FieldParent.ToString() + "';</script>");
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存失败!');</script>");
            return;
        }
    }
}
